<?php

/**
 * 评论
 */
namespace app\order\api;

class CommentApi extends \app\member\api\MemberApi {

    /**
     * 评价商品
     */
    public function push() {
        $id = $this->data['id'];
        $info = target('order/OrderGoods')->getWhereInfo([
            'id' => $id
        ]);
        if (empty($info)) {
            $this->error('该订单商品不存在');
        }
        if ($info['comment_status']) {
            $this->error('该商品已评价!');
        }

        $target = target('order/Order');
        $orderInfo = $target->getInfo($info['order_id']);

        if ($orderInfo['order_user_id'] <> $this->userId) {
            $this->error('该订单无法操作!');
        }

        if($orderInfo['status_data']['action'] <> 'comment') {
            $this->error('暂时无法评价该订单!');
        }

        $level = intval($this->data['level']);
        $level = $level ? $level : 5;
        $content = str_len(html_clear($this->data['content']), 300);
        if (empty($content)) {
            $this->error('请填写评价内容!');
        }
        $images = $this->data['images'];
        if (!empty($images) && is_array($images)) {
            $httpHost = DOMAIN;
            foreach ($images as $image) {
                if (strpos($image, $httpHost, 0) === false) {
                    $this->error('您上传的图片有误,请重新上传!');
                }
            }
        }else {
            $images = [];
        }
        $images = $images ? $images : [];
        $spec = [];
        if (!empty($info['options'])) {
            foreach ($info['options'] as $vo) {
                $spec[] = $vo['value'];
            }
        }
        target('order/OrderComment')->beginTransaction();
        $status = target('order/OrderComment')->add([
            'order_goods_id' => $info['id'],
            'user_id' => $this->userId,
            'app' => $orderInfo['order_app'],
            'has_id' => $info['has_id'],
            'spec' => implode(' ', $spec),
            'time' => time(),
            'content' => $content,
            'level' => $level,
            'images' => serialize($images)
        ]);
        if (!$status) {
            target('order/OrderComment')->rollBack();
            $this->error('商品评价失败,请稍后再试!');
        }
        $status = target('order/OrderGoods')->edit([
            'id' => $id,
            'comment_status' => 1
        ]);
        if (!$status) {
            target('order/OrderComment')->rollBack();
            $this->error('商品评价失败,请稍后再试!');
        }
        $count = target('order/OrderGoods')->countList([
            'order_id' => $orderInfo['order_id'],
            '_sql' => 'id <> ' . $id,
            'comment_status' => 0
        ]);
        if (!$count) {
            $status = target('order/Order')->edit([
                'order_id' => $orderInfo['order_id'],
                'comment_status' => 1
            ]);
            if (!$status) {
                target('order/OrderComment')->rollBack();
                $this->error('商品评价失败,请稍后再试!');
            }
        }
        $model = $orderInfo['order_app'] . '/' . $orderInfo['order_app'];
        $status = target($model)->where([target($model)->getPrimary() => $info['has_id']])->setInc('comments', 1);
        if (!$status) {
            target('order/OrderComment')->rollBack();
            $this->error('商品评价失败,请稍后再试!');
        }
        $status = target($model)->where([target($model)->getPrimary() => $info['has_id']])->setInc('score', $level);
        if (!$status) {
            target('order/OrderComment')->rollBack();
            $this->error('商品评价失败,请稍后再试!');
        }
        target('order/OrderComment')->commit();
        $this->success('商品评价成功!');

    }


}